import Vue from 'vue'
import config from './config.json'
let baseURL = ''

// 获取基础URL（适配UniApp存储API）
export function getBaseURL() {
  return config.baseURL
}


export function getQuickAppsKey() {
  let ip = getBaseURL()
  let userInfo = {}
  let orgInfo = {}
  try {
    userInfo = JSON.parse(uni.getStorageSync('appUserInfo') || '{}')
    orgInfo = JSON.parse(uni.getStorageSync('appOrgInfo') || '{}')
  } catch (e) {
    console.error('解析用户/组织信息失败', e)
  }
  const orgId = orgInfo.orgId || 'unknownOrg'
  const memberId = userInfo.memberId || 'unknownUser'
  return `${ip}_${orgId}_${memberId}`
}
const projectMap = {
  "baojiejianchaapp": "roc-cleaning-inspection-h5",
  "xiaoxitongzhi": "roc-component-message-h5",
  "customer": "roc-customer-service-employee-h5",
  "customer": "roc-customer-service-owner-h5",
  "sf_ygsk": "roc-finance-employee-h5",
  "lvhuajiancha": "roc-green-inspection-h5",
  "yezhushoufangapp": "roc-house-inspecting-h5",
  "fangchanyijiao": "roc-house-transfer-h5",
  "sf_jysk": "roc-operating-receipts-h5",
  "yuyanfangapp": "roc-pre-inspecting-room-h5",
  "pingzhihechaapp": "roc-quality-check-h5",
  "zonghexunjianapp": "roc-quality-inspecting-h5",
  "anfangxungeng": "roc-security-patrol-h5",
  "chengjiejianchaapp": "roc-undertake-inspection-h5",
  "meter_online": "roc-meter-reading-h5",
  "shebeixunweibao": "roc-equipment-maintenance-h5",
  "pinzhi": "roc-quality-standard-h5",
  "rentapp": "roc-rent-employee-h5",
  "ordercenterapp": "roc-workorder-h5",
  "ccgl_wlsl": "roc-house-purch-h5",
  "energymeter": "roc-energy-meter-h5",
  'noticeowner': 'roc-notice-owner-h5'
};

const projectURLMap = {
  indoor_order: 'customer',
  work_area_order: 'customer',
  complain_order: 'customer',
  other_order: 'customer',
  // kh_xmgd: 'customer',
  xiangmugongdanapp: 'customer',
  shoufeikefuapp: 'customer', // 快速报事
  anfangxungeng: 'anfangxungeng', // 安防巡更
  anfangxungengNew: 'anfangxungeng',
  xungengdianweihuapp: 'anfangxungeng',
  anfangxungengzhenggaiapp: 'anfangxungeng',
  anfangxungengjianguanapp: 'anfangxungeng',
  xungengzhenggaijianguanapp: 'anfangxungeng',
  xungengzhenggaijianguanapp: 'anfangxungeng',
  pingzhihechaapp: 'pingzhihechaapp', // 品质核查
  pinzhihechaNew: 'pingzhihechaapp',
  pinzhihechazhenggaiapp: 'pingzhihechaapp',
  pinzhihechajianguanapp: 'pingzhihechaapp',
  pinhezhenggaijianguanapp: 'pingzhihechaapp',
  chengjiejianchaapp: 'pinzhi', // 承接查验
  chengjiechayanNew: 'pinzhi',
  chengjiechayanzhenggaiapp: 'pinzhi',
  chengjiechayanjianguanapp: 'pinzhi',
  chayanzhenggaijianguanapp: 'pinzhi',
  chayanzhenggaitongjiapp: 'pinzhi',
  zonghexunjianapp: 'pinzhi', // 综合巡检
  zonghexunjianNew: 'pinzhi',
  zonghexunjianzhenggaiapp: 'pinzhi',
  zonghexunjianjianguanapp: 'pinzhi',
  zongxunzhenggaijianguanapp: 'pinzhi',
  zongxunzhenggaitongjiapp: 'pinzhi',
  shebeixunweibao: 'pinzhi', // 设备巡维保
  lvhuajiancha: 'pinzhi', // 绿化检查
  lvhuajianchaNew: 'pinzhi',
  lvhuajianchazhenggaiapp: 'pinzhi',
  lvhuajianchajianguanapp: 'pinzhi',
  lvhuazhenggaijianguanapp: 'pinzhi',
  lvhuazhengaitongjiapp: 'pinzhi',
  baojiejianchaapp: 'pinzhi', // 保洁检查
  baojiejianchaNew: 'pinzhi',
  baojiejianchazhenggaiapp: 'pinzhi',
  baojiejianchajianguanapp: 'pinzhi',
  baojiezhenggaijianguanapp: 'pinzhi',
  baojiezhenggaitongjiNew: 'pinzhi',
  shebeibaoyang: 'pinzhi', // 设备保养
  shebeibaoyangNew: 'pinzhi',
  shebeitaizhangapp: 'pinzhi',
  shebeibaoyangweixiuapp: 'pinzhi',
  shebeibaoyangjianguanapp: 'pinzhi',
  baoyangweixiujianguanapp: 'pinzhi',
  baoyangweixiutongjiapp: 'pinzhi',
  shebeixunjian: 'pinzhi', // 设备巡检
  shebeixunjianNew: 'pinzhi',
  shebeixunjianweixiuapp: 'pinzhi',
  shebeixunjianjianguanapp: 'pinzhi',
  xunjianweixiujianguanapp: 'pinzhi',
  xunjiweixiutongjiapp: 'pinzhi',
  fangchanyijiao: 'fangchanyijiao', // 房产移交
  yezhushoufangapp: 'yezhushoufangapp', // 业主收房
  yuyanfangapp: 'yuyanfangapp', // 预验房
  sf_jysk: 'sf_jysk', // 经营收款
  ArrearsManagement: 'sf_ygsk', // 催款管理
  sf_ygsk: 'sf_ygsk', // 员工收款
  shjf: 'sf_ygsk', // 生活缴费
  YAJIN_SHOUKUAN: 'sf_ygsk', // 押金收款
  sf_yjcz: 'sf_ygsk', // 预交充值
  MyInvoice: 'sf_ygsk', // 个人票夹
  ccgl_wlsl: 'ccgl_wlsl', // 仓彩管理/物料申领
  xiaoxitongzhi: 'xiaoxitongzhi', // 消息
  zixunguanliapp: 'zixunguanliapp', // 资讯
  meter_online: 'meter_online', // 移动抄表
  share_meter_online: 'meter_online', // 分摊表抄表
  qualityplanapp: 'qualityplanapp', // 专项计划
  zonghexunjiangerentongjiAPP: 'pinzhi', // 综合巡检个人统计
  shebeixunjiangerentongjiAPP: 'pinzhi', // 设备巡检个人统计
  lvhuajianchagerentongjiAPP: 'pinzhi', // 绿化检查个人统计
  baojiejianchagerentongjiAPP: 'pinzhi', // 保洁检查个人统计
  shebeibaoyanggerentongji: 'pinzhi', // 设备保养个人统计
  chengjiechayangerentongjiAPP: 'pinzhi', // 承接查验个人统计
  zonghexunjiantongjifenxiApp: 'pinzhi', // 综合巡检统计分析
  shebeixunjiantongjifenxiApp: 'pinzhi', // 设备巡检统计分析
  lvhuajianchatongjifenxiApp: 'pinzhi', // 绿化检查统计分析
  baojiejianchatongjifenxiApp: 'pinzhi', // 保洁检查统计分析
  shebeibaoyangtongjifenxiApp: 'pinzhi', // 设备保养统计分析
  chengjiechayantongjifenxiApp: 'pinzhi', // 承接查验统计分析
  zonghexunjianzhenggailuruapp: 'pinzhi', // 综合巡检整改录入
  shebeixunjianzhenggailuruapp: 'pinzhi', // 设备巡检整改录入
  lvhuajianchazhenggailuruapp: 'pinzhi', // 绿化检查整改录入
  baojiejianchazhenggailuru: 'pinzhi', // 保洁检查整改录入
  shebeibaoyangzhenggailuruapp: 'pinzhi', // 设备保养整改录入
  chengjiechayanzhenggailuruapp: 'pinzhi', // 承接查验整改录入
  anfangzhenggailuruapp: 'anfangxungeng', // 安防巡更整改录入
  pinzhizhenggailuruapp: 'pingzhihechaapp', // 品质核查整改录入
  xungenggerentongjiapp: 'anfangxungeng', // 安防巡更个人统计
  xungengtongjifenxiapp: 'anfangxungeng', // 安防巡更统计分析
  pinhegerentongjiapp: 'pingzhihechaapp', // 品质核查个人统计
  pinzhihechatongjifenxiapp: 'pingzhihechaapp', // 品质核查统计分析
  baoyangyewushenheApp: 'pinzhi', // 保养业务审核
  shebeixunjianyewushenheApp: 'pinzhi', // 巡检业务审核
  lvhuayewushenheApp: 'pinzhi', // 绿化业务审核
  zonghexunjianyewushenheApp: 'pinzhi', // 综巡业务审核
  baojieyewushenheApp: 'pinzhi', // 保洁业务审核
  chengjiechayanyewushenheApp: 'pinzhi', // 查验业务审核
  anfangxungengyewushenheApp: 'anfangxungeng', // 安防巡更业务审核
  pinzhihechayewushenheapp: 'pingzhihechaapp', // 品质核查业务审核
  zukongguanli: 'rentapp', // 资产租赁-租控管理
  xiansuoguanli: 'rentapp', // 资产租赁-线索管理
  genjinguanli: 'rentapp', // 资产租赁-跟进管理
  kehudangan: 'rentapp', // 资产租赁-客户档案
  zulingfenxi: 'rentapp', // 资产租赁-租赁分析
  business_chance_app: 'rentapp', // 招商管理-商机管理
  business_chance_follow_app: 'rentapp', // 招商管理-商机跟进
  zulingerentongji: 'rentapp', // 资产租赁-租赁个人统计
  zlht: 'rentapp', // 资产租赁-租赁合同
  order_center_app: 'ordercenterapp', // 工单中心-工单查询
  order_todo_app: 'ordercenterapp', // 工单中心-我的待办
  order_input_app: 'ordercenterapp', // 工单中心-工单录入
  order_approve_app: 'ordercenterapp', // 工单中心-工单审核
  instead_input_app: 'ordercenterapp', // 工单中心-代客录单
  order_handle_app: 'ordercenterapp', // 工单中心-我的经办
  order_draft_app: 'ordercenterapp', // 工单中心-本地草稿
  location_app: 'ordercenterapp', // 工单中心-空间点位
  bkpzx: 'ordercenterapp', // 工单中心-开票中心
  energy_meter_online: 'energymeter', // 工单中心-开票中心
  customer_visit: 'ordercenterapp', // 工单中心-拜访计划
  visitor_scan_code: 'noticeowner', // 访客扫码
  visitor_record: 'noticeowner', // 访客记录
  pass_record: 'noticeowner', // 通行记录
  sjc_gljsc: 'ordercenterapp', // 数据仓-管理驾驶舱
  sjc_wdgztj: 'ordercenterapp', // 数据仓-我的工作统计
  device_app: 'ordercenterapp', // 设备点位
  house_app: 'ordercenterapp', // 房产档案
  CustomerProfileAPP: 'rentapp', // 客户档案
  LLOA_LLOA: 'thirdH5', // 蓝凌OA
  OACS_OACS: 'thirdH5', // 蓝凌OA
  check_report_app: 'ordercenterapp', // 品质核查报告
  door_to_door_questionnaire_survey: 'noticeowner', // 入户调查
  Property_Pass: 'noticeowner', // 放行条管理
  Questionnaire_Statistics: 'noticeowner', // 问卷统计
  rent_contract_tz: 'ordercenterapp', // 退租办理
  rent_contract_hf: 'ordercenterapp', // 换房办理
  rent_contract_xz: 'ordercenterapp', // 续租办理
  property_order_todo_app: 'ordercenterapp', // 工单监管
  property_inspection_app: 'ordercenterapp', // "巡检任务管理"
  property_rectify_app: 'ordercenterapp', // "巡检整改工单"
  _hgjg_app: 'ordercenterapp' // 合规监管
}

function parseCustomApp(appInfo) {
  const { orgId, relationAppId, h5UrlOld, appName } = appInfo
  if (!h5UrlOld) return ''

  const arr = h5UrlOld.split('#/')
  if (h5UrlOld.includes('roc-workorder-h5')) {
    let hash = ''
    if (arr[1] && arr[1].includes('?')) {
      hash = arr[1] + '&appName=' + (appName || '')
    } else if (arr[1]) {
      hash = arr[1] + '?appName=' + (appName || '')
    }
    return `${projectMap[projectURLMap['order_center_app']]}/index.html?orgId=${orgId}&appId=${relationAppId}#/${hash}`
  }
  return ''
}

export function getAppUrl(appCode, code, hashName, appInfo, userInfo = {}) {
  if (appCode === 'shenpi') {
    return ''
  }
  console.log(appInfo, userInfo, 'info')
  const { memberId = '' } = userInfo

  // 解构时增加默认值，避免appInfo不存在导致的错误
  const { orgId = '', relationAppId = '', refDevopsAppId = '', appServiceId = '', appNmae = '', appId = '' } = appInfo || {}

  if (!refDevopsAppId) {
    return parseCustomApp(appInfo)
  }
  console.log(appCode, 'aaaa')
  // return 
  if (typeof projectURLMap[appCode] === 'string') {
    console.log(projectURLMap[appCode], 'module')
    if (appCode === 'xiangmugongdanapp') {
      return `${projectMap[projectURLMap[appCode]]}/index.html?orgId=${orgId}&appId=${relationAppId}&typeapp=1#/projectOrder`
    }
    if (appCode === 'CustomerProfileAPP') {
      return `${projectMap[projectURLMap[appCode]]}/customer.html/#${hashName || ''}?orgId=${orgId}&typeapp=1&appId=${relationAppId}`
    }
    if (appCode === 'MyInvoice') {
      return `${projectMap[projectURLMap[appCode]]}/ticket.html?orgId=${orgId}&typeapp=1&appId=${relationAppId}#${hashName || ''}`
    }
    if (appCode === 'energymeter') {
      return `energymeter/index.html/#${hashName}?orgId=${orgId}&typeapp=1&appId=${relationAppId}#/energyReading`
    }
    if (projectURLMap[appCode] === 'rentapp') {
      if (appCode === 'kehudangan') {
        console.log('dianjikehuguanli1')
        return `/${projectMap[projectURLMap[appCode]]}/#customerRecord?orgId=${orgId}&typeapp=1&appServiceId=${appServiceId}&memberId=${memberId}&appId=${relationAppId}`
      }
      if (appCode === 'xiansuoguanli') {
        return `/${projectMap[projectURLMap[appCode]]}/#clueManagement?orgId=${orgId}&typeapp=1&appServiceId=${appServiceId}&memberId=${memberId}&appId=${relationAppId}`
      }
      if (appCode === 'genjinguanli') {
        return `/${projectMap[projectURLMap[appCode]]}/#followUpManage?orgId=${orgId}&typeapp=1&appServiceId=${appServiceId}&memberId=${memberId}&appId=${relationAppId}`
      }
      if (appCode === 'business_chance_app') {
        return `/${projectMap[projectURLMap[appCode]]}/#commericalChance?orgId=${orgId}&typeapp=1&appServiceId=${appServiceId}&memberId=${memberId}&appId=${relationAppId}`
      }
      if (appCode === 'business_chance_follow_app') {
        return `/${projectMap[projectURLMap[appCode]]}/#chanceFollowup?orgId=${orgId}&typeapp=1&appServiceId=${appServiceId}&memberId=${memberId}&appId=${relationAppId}`
      }

    }
    if (['ordercenterapp', 'noticeowner', 'meter_online', 'ccgl_wlsl', 'sf_ygsk'].includes(projectURLMap[appCode])) {
      if (appCode === 'property_order_todo_app' || appCode === 'property_rectify_app') {
        return `/${projectMap[projectURLMap[appCode]]}/#${hashName}&orgId=${orgId}&typeapp=1&appServiceId=${appServiceId}&memberId=${memberId}&appId=${relationAppId}`
      }
      if(appNmae === '退租办理'){
          return `/${projectMap[projectURLMap[appCode]]}/#${hashName}&orgId=${orgId}&typetitle=1&typeapp=1&appServiceId=${appServiceId}&memberId=${memberId}&appId=${relationAppId}`
      }
      if(appNmae === '换房办理'){
          return `/${projectMap[projectURLMap[appCode]]}/#${hashName}&orgId=${orgId}&typetitle=2&typeapp=1&appServiceId=${appServiceId}&memberId=${memberId}&appId=${relationAppId}`
      }
      if(appNmae === '续租办理'){
          return `/${projectMap[projectURLMap[appCode]]}/#${hashName}&orgId=${orgId}&typetitle=3&typeapp=1&appServiceId=${appServiceId}&memberId=${memberId}&appId=${relationAppId}`
      }
      if (hashName) {
        return `/${projectMap[projectURLMap[appCode]]}/#${hashName}?orgId=${orgId}&typeapp=1&appServiceId=${appServiceId}&memberId=${memberId}&appId=${relationAppId}`
      }
    }
    if (['anfangxungeng', 'pingzhihechaapp', 'pinzhi', 'energymeter',].includes(projectURLMap[appCode])) {
      console.log(hashName, 'hashname')
      if (hashName) {
        return `/${projectMap[projectURLMap[appCode]]}/index.html/#${hashName}?orgId=${orgId}&typeapp=1&appServiceId=${appServiceId}&memberId=${memberId}&appId=${relationAppId}`
      }
      return `/${projectMap[projectURLMap[appCode]]}/index.html?orgId=${orgId}&typeapp=1&appId=${relationAppId}&memberId=${memberId}&appServiceId=${appServiceId}`
    }
    return `/${projectMap[projectURLMap[appCode]]}/index.html?orgId=${orgId}&typeapp=1&memberId=${memberId}&appId=${relationAppId}&appServiceId=${appServiceId}`
  } else {
    return ''
  }
}

export function changeBaseURL(ip) {
  let useHttps = uni.getStorageSync('serverUseHttps')
  let pre = useHttps === 'true' ? 'https' : 'http'
  baseURL = `${pre}://${ip}`
  Vue.prototype.$baseURL = baseURL
  uni.setStorageSync('baseURL', baseURL)
}

Vue.prototype.$baseURL = baseURL

